/******************************************************************************
*
* This file is part of FreeTrack (http://code.google.com/p/qtfreetrack).
* BY Willy Gardiol (willy@gardiol.org)
*
* FreeTrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FreeTrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar.  If not, see <http://www.gnu.org/licenses/>
*
******************************************************************************/

#ifndef FTLIST_H
#define FTLIST_H

#include <QtGui>

#define FTL_INVALID_KEY -9000000
#define FTL_BASE_KEY -6000000

class FTList : public QWidget
{
    Q_OBJECT
public:
    explicit FTList(QWidget *parent = 0);

    // Remove all items from the list
    void clear();

    // highlit items
    void highlightClear();
    void highlightKey(int key, bool h = true);
    void highlightId(int id, bool h = true);
    QList<int> keys() const;

    // Add string(s) to the list
    void appendString( const QString&, int key );
    void appendString( const QString& );
    void appendStrings( const QStringList& );

    // Manage strings using KEYs
    void updateString(int key, const QString&);
    void deleteString(int key);

    // Manage strings using indexes
    void updateStringIndex(int index, const QString&);
    void deleteStringIndex(int index);

    // Key to index and vicevers
    int keyToIndex(int key ) const;
    int indexToKey(int index) const;

    // Manage events (for kinetik scroll)
    void mousePressEvent(QMouseEvent *);
    void mouseMoveEvent(QMouseEvent *);
    void mouseReleaseEvent(QMouseEvent *);
    void paintEvent(QPaintEvent *);

    // Return selected item
    QString selectedString() const;
    int selectedIndex() const;
    int selectedKey() const;

    QString stringFromKey(int) const;
    QString stringFromIndex(int) const;

signals:
    // Emit when a string is selected
    void stringSelected(const QString&);
    void indexSelected(int);
    void keySelected(int);

public slots:
    void selectString(QString);
    void selectIndex(int);
    void selectKey(int);

private:
    // Are we scrolling?
    bool scrolling;
    // Store mouse Y position during scrolling
    int scroll_start;
    // Screen scroll offset
    int scroll_offset;

    // Store the height of a row
    int row_height;

    // Store selected row
    int _selectedIndex;

    // The items
    QStringList strings;
    QList<int> keysMap;
    QList<bool> highlighted;
    int baseKey;

    void scrollToFit();
};

#endif // FTLIST_H
